package com.newjiabo.ski.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.newjiabo.common.utils.DateUtils;
import com.newjiabo.common.utils.SnowFlake;
import com.newjiabo.common.utils.StringUtils;
import com.newjiabo.ski.domain.SkiMealSnowRel;
import com.newjiabo.ski.domain.SkiSetMeal;
import com.newjiabo.ski.domain.SkiSnowField;
import com.newjiabo.ski.domain.dto.SkiSnowFieldDTO;
import com.newjiabo.ski.domain.enums.SnowStatusEnum;
import com.newjiabo.ski.domain.vo.SkiSnowFieldInfoVo;
import com.newjiabo.ski.domain.vo.SnowFieldQuery;
import com.newjiabo.ski.mapper.SkiMealSnowRelMapper;
import com.newjiabo.ski.mapper.SkiSnowFieldMapper;
import com.newjiabo.ski.service.ISkiMealSnowRelService;
import com.newjiabo.ski.service.ISkiSetMealService;
import com.newjiabo.ski.service.ISkiSnowFieldInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 雪场信息Service业务层处理
 *
 * @author CaoMengHui
 * @date 2023-09-18
 */
@Service
public class SkiSnowFieldInfoServiceImpl implements ISkiSnowFieldInfoService {
    @Resource
    private SkiSnowFieldMapper skiSnowFieldMapper;
    @Resource
    private SkiMealSnowRelMapper skiMealSnowRelMapper;
    @Resource
    private ISkiSetMealService setMealService;
    @Resource
    private ISkiMealSnowRelService mealSnowRelService;

    /**
     * 查询雪场信息
     *
     * @param id 雪场信息主键
     * @return 雪场信息
     */
    @Override
    public SkiSnowField selectSkiSnowFieldById(String id) {
        return skiSnowFieldMapper.selectSkiSnowFieldById(id);
    }

    @Override
    public SkiSnowFieldInfoVo selectSkiSnowFieldInfoById(String id) {
        SkiSnowFieldInfoVo skiSnowFieldInfoVo = new SkiSnowFieldInfoVo();
        SkiSnowField skiSnowField = this.selectSkiSnowFieldById(id);
        List<SkiMealSnowRel> skiMealSnowRels = skiMealSnowRelMapper.selectSkiMealSnowRelBySnowFieldId(id);
        List<String> setMealIds = skiMealSnowRels.stream().map(ski -> ski.getSetMealId()).collect(Collectors.toList());
        skiSnowFieldInfoVo.setSkiSnowField(skiSnowField);
        if (CollectionUtil.isNotEmpty(setMealIds)) {
            List<SkiSetMeal> skiSetMealsByIds = setMealService.selectByIds(setMealIds);
            skiSnowFieldInfoVo.setSkiSetMealList(skiSetMealsByIds);
        }
        return skiSnowFieldInfoVo;
    }

    /**
     * 查询雪场信息列表
     *
     * @param skiSnowField 雪场信息
     * @return 雪场信息
     */
    @Override
    public List<SkiSnowField> selectSkiSnowFieldList(SkiSnowField skiSnowField) {
        return skiSnowFieldMapper.selectSkiSnowFieldList(skiSnowField);
    }

    /**
     * 新增雪场信息
     *
     * @param skiSnowField 雪场信息
     * @return 结果
     */
    @Override
    public int insertSkiSnowField(SkiSnowField skiSnowField) {
        if (StringUtils.isEmpty(skiSnowField.getId())) {
            skiSnowField.setId(SnowFlake.getIdStr());
        }
        skiSnowField.setCreateTime(DateUtils.getNowDate());
        skiSnowField.setDelFlag(false);
        return skiSnowFieldMapper.insertSkiSnowField(skiSnowField);
    }

    /**
     * 修改雪场信息
     *
     * @param skiSnowField 雪场信息
     * @return 结果
     */
    @Override
    public int updateSkiSnowField(SkiSnowField skiSnowField) {
        skiSnowField.setUpdateTime(DateUtils.getNowDate());
        return skiSnowFieldMapper.updateSkiSnowField(skiSnowField);
    }

    /**
     * 批量删除雪场信息
     *
     * @param ids 需要删除的雪场信息主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteSkiSnowFieldByIds(String[] ids) {
        // 删除雪场信息之前删除关联关系
        skiMealSnowRelMapper.deleteSkiMealSnowRelBySnowFieldIds(ids);
        return skiSnowFieldMapper.deleteSkiSnowFieldByIds(ids);
    }

    /**
     * 删除雪场信息信息
     *
     * @param id 雪场信息主键
     * @return 结果
     */
    @Override
    public int deleteSkiSnowFieldById(String id) {
        return skiSnowFieldMapper.deleteSkiSnowFieldById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addSnowField(SkiSnowFieldDTO skiSnowFieldDTO) {
        String snowFieldId = SnowFlake.getIdStr();
        this.initSnowField(skiSnowFieldDTO);
        this.initMealSnowRel(skiSnowFieldDTO, snowFieldId);
        SkiSnowField skiSnowField = BeanUtil.copyProperties(skiSnowFieldDTO, SkiSnowField.class);
        skiSnowField.setId(snowFieldId);
        return this.insertSkiSnowField(skiSnowField);

    }

    private void initSnowField(SkiSnowFieldDTO skiSnowFieldDTO) {
        if (StringUtils.isEmpty(skiSnowFieldDTO.getSnowStatus())) {
            skiSnowFieldDTO.setSnowStatus(SnowStatusEnum.OPEN.name());
        }
    }

    private void initMealSnowRel(SkiSnowFieldDTO skiSnowFieldDTO, String snowFieldId) {
        List<String> setMealIds = skiSnowFieldDTO.getSetMealIds();
        if (CollectionUtil.isNotEmpty(setMealIds)) {
            List<SkiMealSnowRel> skiMealSnowRelList = new ArrayList<>();
            for (String setMealId : setMealIds) {
                skiMealSnowRelList.add(new SkiMealSnowRel(snowFieldId, setMealId));
            }
            mealSnowRelService.saveBatch(skiMealSnowRelList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateSnowField(String snowFieldId, SkiSnowFieldDTO skiSnowFieldDTO) {
        this.initMealSnowRel(skiSnowFieldDTO, snowFieldId);
        SkiSnowField skiSnowField = BeanUtil.copyProperties(skiSnowFieldDTO, SkiSnowField.class);
        skiSnowField.setId(snowFieldId);
        return this.updateSkiSnowField(skiSnowField);
    }

    @Override
    public List<SkiSnowField> selectByQuery(SnowFieldQuery query) {
        List<String> snowAddressIdPathList = query.getSnowAddressIdPath();
        if (CollectionUtil.isNotEmpty(snowAddressIdPathList)) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String id : snowAddressIdPathList) {
                stringBuilder.append(id);
                stringBuilder.append("|");
            }
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
            query.setSnowAddressIdPathStr(stringBuilder.toString());
        }
        return skiSnowFieldMapper.selectByQuery(query);
    }
}
